import { getListForPage, getById, createCustoms, updateCustoms, deleteCustoms } from '../../services/basedata/customs';
import {message} from 'antd';
const R=require("ramda");

export default {
    namespace: 'customs',
    state: {
        page: {},
        list: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }){
            return {...state, ...payload };
        }
    },
    effects: {

        /**
         * 分页获取海关列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getListForPage({ payload }, { call, put, select }) {

            const res = yield call(getListForPage, { ...payload });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: content.list,
                        page: content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取单个租户市场采购个体工商户
         * @param payload
         * @param call
         * @param put
         */
            *getById({ payload }, { call, put }){

            if(payload.id){
                const res = yield call(getById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if(res && res.data.resultCode === "ok"){
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if(payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },



        /**
         * 保存海关信息
         * @param payload
         * @param call
         */
            *createCustoms({ payload },{ call }){
            const res = yield call(createCustoms, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("海关信息新增成功！");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },


        /**
         * 更新海关信息
         * @param payload
         * @param call
         * @param put
         */
            *updateCustoms({ payload },{ call, put }){
            const res = yield call(updateCustoms, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if(res && res.data.resultCode === "ok"){
                message.info("海关信息修改成功！");
                if(payload.onSuccess) payload.onSuccess();
            } else {
                if(payload.onFail) payload.onFail();
            }
        },


        /**
         * 删除海关信息
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *deleteCustoms({ payload }, {call, put, select}) {
            const res = yield call(deleteCustoms, { id: payload.id });
            if (res.data.resultCode === "ok") {
                message.info("删除海关信息成功！");
                if(payload.onSuccess)payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

    }
};
